"""
Author: Faye Lee
Date: 2025-04-12 11:49:26
LastEditTime: 2025-05-08 18:21:20
LastEditors: Faye Lee
Description:
FilePath: \pygame_alien\alien_invasion.py
Halo Baby,欢迎打开新世界的大门!
"""

import pygame
import game_functions as gf
from settings import Settings
from pygame.sprite import Group
from ship import Ship
from alien import Alien
from game_stats import GameStats
from button import Button
from scoreboard import Scoreboard
from fps import FPS


def run_game():
    # 初始化pygame、设置和屏幕对象
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height)
    )
    fps = FPS(ai_settings)

    pygame.display.set_caption("外星人大战")

    # 创建一个用于存储游戏统计信息的实例
    stats = GameStats(ai_settings)
    sb = Scoreboard(ai_settings, screen, stats, fps)

    # 创建一艘飞船
    ship = Ship(ai_settings, screen)
    # 创建一个用于存储子弹的编组
    bullets = Group()
    # 创建一个用于存储外星人的编组
    aliens = Group()
    # 创建一群外星人
    gf.create_fleet(ai_settings, screen, ship, aliens)
    # 创建Play按钮
    play_button = Button(ai_settings, screen, "Play Game")

    # 开始游戏的主循环
    while True:
        # 限制游戏的最大帧率
        fps.tick()
        # 监听键盘和鼠标事件
        gf.check_events(
            ai_settings, screen, stats, sb, play_button, ship, aliens, bullets
        )

        # 如果游戏处于活动状态,目前是看生命值够不够
        if stats.game_active:
            # print(stats.ships_lives, stats.game_active)
            # 更新飞船的位置
            ship.update()
            # 更新子弹的位置
            gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets)

            # 更新外星人的位置
            gf.update_aliens(ai_settings, stats, sb, screen, ship, aliens, bullets)

        # 更新屏幕
        gf.update_screen(
            ai_settings, screen, stats, sb, ship, aliens, bullets, play_button
        )


run_game()
